package com.xm.settlement.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xm.settlement.commons.result.Tree;
import com.xm.settlement.mapper.AppGoodsTypeMapper;
import com.xm.settlement.model.AppGoodsType;
import com.xm.settlement.service.IAppGoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品类别 表数据服务层接口实现类
 * Created by Administrator on 2017/3/1.
 */
@Service
public class AppGoodsTypeServiceImpl extends SuperServiceImpl<AppGoodsTypeMapper,AppGoodsType> implements IAppGoodsTypeService {

    @Autowired
    private AppGoodsTypeMapper appGoodsTypeMapper;

    @Autowired
    private IAppGoodsTypeService appGoodsTypeService;

    @Override
    public List<Tree> selectTree() {
        List<Tree> trees = new ArrayList<Tree>();

        List<AppGoodsType> appGoodsTypeFather = appGoodsTypeMapper.selectByPIdNull();

        if (appGoodsTypeFather != null) {
            for (AppGoodsType appGoodsTypeOne : appGoodsTypeFather) {
                Tree treeOne = new Tree();

                treeOne.setId(appGoodsTypeOne.getId());
                treeOne.setText(appGoodsTypeOne.getName());
                treeOne.setIconCls(appGoodsTypeOne.getIcon());

                List<AppGoodsType> appGoodsTypeSon = appGoodsTypeMapper.selectAllByPId(appGoodsTypeOne.getId());

                if (appGoodsTypeSon != null) {
                    List<Tree> tree = new ArrayList<Tree>();
                    for (AppGoodsType appGoodsTypeTwo : appGoodsTypeSon) {
                        Tree treeTwo = new Tree();
                        treeTwo.setId(appGoodsTypeTwo.getId());
                        treeTwo.setText(appGoodsTypeTwo.getName());
                        treeTwo.setIconCls(appGoodsTypeTwo.getIcon());
                        //-------------------------------------------------




                        tree.add(treeTwo);
                    }
                    treeOne.setChildren(tree);
                } else {
                    treeOne.setState("closed");
                }
                trees.add(treeOne);
            }
        }
        return trees;
    }

    @Override
    public List<AppGoodsType> selectTreeGrid() {
        EntityWrapper<AppGoodsType> wrapper = new EntityWrapper<AppGoodsType>();
        return appGoodsTypeMapper.selectList(wrapper);
    }

    @Override
    public Integer deleteByCondition(Long id) {
        int count = appGoodsTypeMapper.selectCountById(id);
        if(count > 0){
            return 0; //请先删除分类下商品
        }else{
            appGoodsTypeService.deleteById(id);
            return 1; //删除成功
        }
    }
}
